-----------------------------------------------------------------------------
-- 设置语音选择界面
--
---------------
-- Author: ZhengFeng
-- Date: 2021.01.21
-----------------------------------------------------------------------------
require "Common/define"
require "Logic/Config"

Page_VoiceChangeLanguage = UIBase.New(PanelResNames.Page_VoiceChangeLanguage)
local this = Page_VoiceChangeLanguage

function this.Init(callback)
    this.tempFunc = callback
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)
    self.Btn_Confirm = self:GetChild("Btn_Confirm")
    self:RegisterOnClick(self.Btn_Confirm,self.onClickConfirm)
    self.Txt_Title = self:GetChild("Txt_Title")
    self.List_VoiceLanguage = self:GetChild("List_VoiceLanguage")
    self.List_VoiceLanguage.itemRenderer = this.renderVoiceLanguage
end

function this:OnEnable()
    if this.tempFunc then
        this.callBackFunc = this.tempFunc
        this.tempFunc = nil
    end
    self.currentLanguage = AudioManager.CurrentLanguage
    self.List_VoiceLanguage.numItems = #AudioManager.VoiceLanguageTable
    self.List_VoiceLanguage.onClickItem:Add(this.onItemClickVoiceLanguage)
end

function this:OnDisable()
    if this.callBackFunc then
        this.callBackFunc()
        this.callBackFunc = nil
    end
    self.List_VoiceLanguage.onClickItem:Remove(this.onItemClickVoiceLanguage)
end
function this:OnDestroy()
    self.currentLanguage = nil
end

function this:onClickClose()
    local bankName = AudioManager.GetLanguageBank()
	AudioManager.LoadBankAsync(bankName,function() 
        closeUI(PanelResNames.Page_VoiceChangeLanguage)
        AudioManager.LastPlayInteractionEventName = nil
        AudioManager.LastPlayInteractionGO = nil
    end)
end

function this:onClickConfirm()
    AudioManager.SetCurrentLanguage(self.currentLanguage)
    local _languageStr = ""
    for _, value in ipairs(AudioManager.VoiceLanguageTable) do
        if self.currentLanguage == value.Tag then
            _languageStr = getLanguage(value.Name)
            break
        end
    end
    local _settingStr = string.format(getLanguage("Sys_MsgVoiceChanged"),_languageStr)
    UISysTips.AddMsg(_settingStr)
	this:onClickClose()
end

function this.renderVoiceLanguage(index, obj)
    local _index = index+1
    local _voiceLanguageInfo = AudioManager.VoiceLanguageTable[_index]
    obj:GetChild("Txt_LanguageName").text = getLanguage(_voiceLanguageInfo.Name)
    if this.currentLanguage == _voiceLanguageInfo.Tag then
        obj.selected = true
    else
        obj.selected = false
    end
end

function this.onItemClickVoiceLanguage(context)
    local _obj = context.data
    local _index = this.List_VoiceLanguage:GetChildIndex(_obj) + 1
    local _voiceLanguageInfo = AudioManager.VoiceLanguageTable[_index]
    this.currentLanguage = _voiceLanguageInfo.Tag
end

return Page_VoiceChangeLanguage